void chushihuaPWM()
{
    ledcSetup(ledChannel, freq, resolution); // PWM初始化
    ledcAttachPin(PWM_PIN, ledChannel);      // 绑定PWM通道
    ledcWrite(ledChannel, 0);                // PWM占空比初始化
    pinMode(tyn_PIN, OUTPUT);                // 设置引脚为 输出模式
    pinMode(SD_PIN, OUTPUT);                 // 设置引脚为 输出模式
}

void qidong_tiaoyaqi()
{

    if (in && adc == 0 && MOS_T <= T)
    {
        if (xdc_U <= Ux && tyn_U >= Utg && tyn_I <= It)
        {
            Duty++;
            if (Duty >= 255)
            {
                Duty = 255;
            }
            ledcWrite(ledChannel, Duty); // 设置 PWM 输出的占空比
        }
        else
        {
            Duty--;
            if (Duty <= 0)
            {
                Duty = 0;
            }
            ledcWrite(ledChannel, Duty); // 设置 PWM 输出的占空比
        }
    }
}
unsigned long preHeartTick4 = 0; // 心跳

void qidong_mppt()
{
    static int A;
    static int B;
    static bool mppt = false;

    if (mppt_kg && in && millis() - preHeartTick4 >= 500)
    {
        if (mppt == false)
        {
            mppt = true;
            Utg = Utg - s;
            A = tyn_U * tyn_I;
        }
        else
        {
            mppt = false;
            Utg = Utg + s;
            B = tyn_U * tyn_I;
        }
        int P = A - B;
        if (P > 10000 && Utg <= 18000)
        {
            Utg = Utg + s;
        }
        else if (P < 10000 && Utg >= 13000)
        {
            Utg = Utg - s;
        }
        if (xdc_U >= Ux && xdc_I <= Ixm / 1000 && mppt_cdzt == true)
        {
            digitalWrite(spk_PIN, HIGH);
            delay(500);
            digitalWrite(spk_PIN, LOW);
            delay(500);
            digitalWrite(spk_PIN, HIGH);
            delay(500);
            digitalWrite(spk_PIN, LOW);
            Serial.println("已经冲满电,开启浮充");
            Ux = Uxf;
            mppt_cdzt = true;
        }
        else if (xdc_U <= Uxc && Ux == Uxf && mppt_cdzt == false)
        {
            digitalWrite(spk_PIN, HIGH);
            delay(500);
            digitalWrite(spk_PIN, LOW);
            delay(500);
            digitalWrite(spk_PIN, HIGH);
            delay(500);
            digitalWrite(spk_PIN, LOW);
            Serial.println("关闭浮充,开启MPPT充电模式");
            Ux = Uxm;
            mppt_cdzt = false;
        }
        preHeartTick4 = millis();
    }
}

unsigned long preHeartTick0 = 0; // 心跳
void qidongMPPT()
{
    static int Utb;
    static int ok = 0;
    static bool q = true;
    static bool t1 = true;

    if (millis() - preHeartTick0 >= KEEPALIVEATIME)
    {

        if (out && (tyn_I > 0 || tyn_U > (xdc_U + 1000)))
        {
            if (ok < 10)
            {
                ok++;
            }
            if (in == false && ok == 10)
            {
                in = true;
                Utb = Utq;
                digitalWrite(tyn_PIN, HIGH);
                digitalWrite(SD_PIN, HIGH);
                Serial.println("打开控制器");
                sendtoTCPServer("cmd=2&uid=" + UID + "&topic=" + TOPIC + "&msg=打开控制器\r\n"); // 推送消息
                if (q == false)
                {
                    q = true;
                    Q = 0;
                    sendtoTCPServer("cmd=2&uid=" + UID + "&topic=" + TOPIC + "&msg=容量清零\r\n"); // 推送消息
                    operateOnDataInFile("/data.txt", "Q", "W", Q);
                }
            }
            else if (q == true && ok == 10)
            {
                q = false;
            }
        }
        else
        {
            if (ok > 0)
            {
                ok--;
            }
            else if (in == true)
            {
                in = false;
                Utb = 16000;
                digitalWrite(tyn_PIN, LOW);
                digitalWrite(SD_PIN, LOW);
                Serial.println("关闭控制器");
                sendtoTCPServer("cmd=2&uid=" + UID + "&topic=" + TOPIC + "&msg=关闭控制器\r\n"); // 推送消息
            }
        }
        if (t1 && 40 < MOS_T)
        {
            t1 = false;
            digitalWrite(23, HIGH); // 打开风扇
        }
        else if (t1 == false && 40 > MOS_T)
        {
            digitalWrite(23, LOW); // 关闭风扇
            t1 = true;
        }

        Ux = Uxm + tyn_I / 100; // 线损补偿电压
        preHeartTick0 = millis();
    }
    qidong_tiaoyaqi();
    qidong_mppt();
}